<?php 
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
ob_start("ob_gzhandler");
require('../includes/init.php');
require('./classes/control/control_ad_position.php');

//广告位模块
class AdPosition {
	
	private  $ad_position_c;
	private $w_type;
	/**
	* @ 构造函数,实例化adControl
	* @ __construct
	*/
	public function __construct() {
		
		$this->ad_position_c = new AdPositionControl();
		$this->w_type = (int) $_REQUEST['w_type'];
	}
	/**
	* @函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	public function index() {
		
		$url_info = $ad_info = array();

		$page = !empty($_REQUEST['page']) ? (int) $_REQUEST['page'] : 1;//分页处理
		$page_size = 20;
		$url_info = $this->ad_position_c->getUrlInfo($page, 'list_index', $page_size, $this->w_type);//得到分页信息
		
		$position_info = $this->ad_position_c->getAdPositionInfo('list_index', 0, $this->w_type, $url_info['page_info']['start'], $page_size);//得到相应分页数据
		
		$GLOBALS['smarty']->assign('web_info', get_web_info($this->w_type));
		$GLOBALS['smarty']->assign('w_type', $this->w_type);
		$GLOBALS['smarty']->assign('page_url', $url_info['page_url']);
		$GLOBALS['smarty']->assign('position_info', $position_info );
		$GLOBALS['smarty']->assign('total', $url_info['page_info']['total']);
		$GLOBALS['smarty']->display('ad_position_index.htm'); 
	
	}
	/**
	* @函数add_edit//增加，修改
	* @param  NULL
	* @return  string
	*/
	public function addEdit() {

		$position_id = (int) $_REQUEST['position_id'];
		
		if ( $_POST['act'] ) {
			
			$array = array( 'position_name' =>  trim($_POST['position_name']),
							'position_size' => trim($_POST['position_size']),
							'position_type' => trim($_POST['position_type']),
							'position_description' => trim($_POST['position_description']),
							'position_id' => (int) $_POST['position_id'],
							'position_location' =>(int) $_POST['position_location']
						);
					
			if ( !empty($position_id) ) {

				$rows = $this->ad_position_c->operate_ad_position('edit', $array, $this->w_type);
				
				if ($rows['affect_nums'] > 0) {
					echo  '<script type="text/javascript"> 
								alert("编辑成功！");
								window.location.href="/wondershare/ad_position.php?action=add_edit&act=edit&w_type='.$this->w_type.'&position_id='.$position_id.'"; 
							</script>';
					exit;
				}
			} else {
				
				$rows = $this->ad_position_c->operate_ad_position('add', $array, $this->w_type);
				if ($rows['affect_nums'] > 0) {
					echo  '<script type="text/javascript"> alert("增加成功！");
					window.location.href="/wondershare/ad_position.php?action=add_edit&act=edit&w_type='.$this->w_type.'&position_id='.(int)$rows['last_id'].'"; </script>';
					exit;
				}
			}
		}
					
		if ( !empty($position_id) ) {
			$position_info = $this->ad_position_c->getAdPositionInfo('position_edit', $position_id, $this->w_type);
			
			$GLOBALS['smarty']->assign('position_info', $position_info );
			$GLOBALS['smarty']->assign('title', '修改');
			$GLOBALS['smarty']->assign('form_act', 'edit');
		} else {
			$GLOBALS['smarty']->assign('title', '添加');
			$GLOBALS['smarty']->assign('form_act', 'add');
		}
		
		$GLOBALS['smarty']->assign('web_info', get_web_info($w_type));
		$GLOBALS['smarty']->assign('w_type', $this->w_type);
		$GLOBALS['smarty']->display('ad_position_info.htm');
	}
	
	/**
	* @函数ajaxAd ajax请求广告位处理
	* @param  NULL
	* @return  string
	*/
	public function ajaxAdPostion() {
		
		$type = trim($_GET['type']);
		
		switch ($type) {
			
			case 'is_name_exist':
				$position_id = (int) $_GET['position_id'];
				$position_name = trim($_GET['position_name']);
				$nums = $this->ad_position_c->isPositionNameExist($position_id, $position_name, $this->w_type);
				echo  $nums;
			  break;  
			  
			case 'update_position_name':
			  	
			  	$position_id = (int) $_GET['position_id'];
				$data = array(	'position_name' => trim($_GET['position_name']),
								'update_time' => $_SERVER['REQUEST_TIME'],
								'update_user' => $_SESSION['AName'],
						);
			
				$nums = $this->ad_position_c->isPositionNameExist($position_id, $data['position_name'], $this->w_type);
				
				if ( empty($nums) ) {
					
					$lines = $GLOBALS['db']->update('ad_position', $data, " position_id = {$position_id} ");
					
					echo $msg =  $lines == 1 ? '修改成功' : '修改失败'; 
					
				} else {
					echo '广告位名称已经存在!';
				}
				
			  break;  
			  
			case 'del_ad_postion':
				$data = array('is_delete' => 1);
				$position_id_str = substr($_GET['position_id_str'], 0, (trim(strlen($_GET['position_id_str'])) - 1) );
				$lines = $GLOBALS['db']->update('ad_position', $data, " FIND_IN_SET(position_id, '{$position_id_str}') ");
			  break;  
			
			default:
			  echo '请求类型错误，木有找到';
		}
		exit;
	}
}

$action    = $_REQUEST['action']; 
$ad_position   = new AdPosition;
if ( method_exists($ad_position, $action) )  {
	$ad_position->$action();
} else {
	$ad_position->index();
}

?>
